package persistence.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import persistence.exception.DAOException;

public class TestConnectionProvider implements ConnectionProvider{

//	private Connection con = null; --> doesn't work, because in DAOJdbcImpls Connection is closed, although this 
	
	public Connection getConnection() throws DAOException {
		Connection con = null;
		Properties props = new Properties();
		try {
			props.load(new FileInputStream("db.test.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String user = props.getProperty("db.user");
		String password = props.getProperty("db.password");
		String connectionString = props.getProperty("db.url");
		String driverName = props.getProperty("db.driver-class");
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			con = DriverManager.getConnection(connectionString, user, password);
			//			con.setAutoCommit(false);
		} catch (SQLException e) {
			throw new DAOException("getConnection(): connection not retrieved");
		}
		return con;
	}
}